package com.example.pingtools.base

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context
import android.opengl.Visibility
import android.text.Editable
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import com.example.mygreendao.GreenDaoManager
import com.example.pingtools.R
import com.example.pingtools.bean.IpDataBean
import com.example.pingtools.bean.isupdate
import com.example.pingtools.util.PingUtil
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item_ip.*
import kotlinx.android.synthetic.main.item_ip.view.*
import kotlinx.android.synthetic.main.layout_addip.*
import kotlinx.android.synthetic.main.layout_addip.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import java.lang.Exception
import java.net.ConnectException
import java.net.InetSocketAddress
import java.net.Socket

@SuppressLint("WrongViewCast")
class dialog(context: Context, id: Long?, position:Int, iplist:MutableList<IpDataBean>?) : AlertDialog(context),
    View.OnClickListener {
    var floderid:Long?=null
    var position:Int=0
    var iplist:MutableList<IpDataBean>?=null
    init {
        var layout = LayoutInflater.from(context).inflate(R.layout.layout_addip, null)

        setView(layout)

        //设置点击别的区域关闭页面
        setCancelable(true)

        layout.tv_start.setOnClickListener(this)
        layout.tv_cancle.setOnClickListener(this)
        floderid=id
        this.iplist=iplist
        this.position=position
        if(iplist!=null){
            layout.tv_start.setText("修改")
            layout.etbj_ip.text=Editable.Factory.getInstance().newEditable(iplist.get(position).ip)
            layout.etbj_port.text=Editable.Factory.getInstance().newEditable(iplist.get(position).port)
            layout.et_bz.text=Editable.Factory.getInstance().newEditable(iplist.get(position).text)
        }

    }

    override fun show() {
        val params: WindowManager.LayoutParams = window!!.attributes
        params.width = 700
        params.gravity = Gravity.CENTER
        window!!.attributes = params
        window!!.setBackgroundDrawableResource(android.R.color.transparent)
        super.show()
    }

    override fun onClick(v: View?) {
        when (v) {
            tv_start ->{
                var etip = etbj_ip.text
                var etport: Int = 0
                if (etbj_port.text!!.toString() != null && etbj_port.text!!.toString() != "") {
                    etport = etbj_port.text.toString().toInt()
                }
                if(iplist!=null){
                    var ipbean = IpDataBean()
                   // Log.d("fffffff",iplist!!.get(position).id.toString()+"---"+iplist!!.get(position).port)
                    ipbean.id=iplist!!.get(position).id
                    ipbean.text=  if (et_bz.text.toString() == null||et_bz.text.toString()=="") iplist!!.get(position).text else et_bz.text.toString()
                    ipbean.ip= if (etip.toString() == null||etip.toString()=="") iplist!!.get(position).ip.toString() else etip.toString()
                    ipbean.port= if (etport.toString()=="0") iplist!!.get(position).port else etport.toString()
                    /*ipbean.text = et_bz.text.toString()
                    ipbean.ip = etip.toString()
                    ipbean.port = etport.toString()*/
                    ipbean.customId = floderid
                    GreenDaoManager(context).updateip(ipbean)
                    EventBus.getDefault().postSticky(isupdate(2, false,0))
                    dismiss()
                }else {
                    var isping = PingUtil().startPing("$etip")
                    tv_ipstatus.visibility = View.VISIBLE
                    tv_portstatus.visibility = View.VISIBLE
                    if (isping) {
                        tv_ipstatus.text = "$etip 连接正常"
                        // tv_ipstatus.setTextColor(R.color.cardview_shadow_end_color)
                    } else {
                        tv_ipstatus.text = "$etip 连接失败"
                       // tv_portstatus.visibility = View.GONE
                    }
                    if (etbj_port.text!!.toString() != null && etbj_port.text!!.toString() != "") {
                        isPortOpen("$etip", etport, 2000)
                    }
                    var ipbean = IpDataBean()
                    ipbean.text = et_bz.text.toString()
                    ipbean.ip = etip.toString()
                    ipbean.port = etport.toString()
                    ipbean.customId = floderid
                    GreenDaoManager(context).addip(ipbean)
                    EventBus.getDefault().postSticky(isupdate(2, false,0))
                }
                dismiss()
            }
            tv_cancle -> {
                dismiss()
            }
            }

        }


    public fun isPortOpen(ip: String?, port: Int, timeout: Int) {
        var isopen: Boolean = false
        GlobalScope.launch(Dispatchers.IO) {
            try {
               // Log.d("rrrrrrrrr", "$ip $port")
                val socket = Socket()
                if(ip==null||ip==""){
                    socket.connect(InetSocketAddress(port), timeout)
                }else {
                    socket.connect(InetSocketAddress(ip, port), timeout)
                }
                socket.close()
                isopen = true
            } catch (ce: ConnectException) {
                ce.printStackTrace()
               // Log.d("rrrrrrrrr", "1")
                isopen = false
            } catch (ex: Exception) {
                ex.printStackTrace()
               // Log.d("rrrrrrrrr", "2")
                isopen = false
            }
            var dk = port.toString()
            if (isopen) {
                tv_portstatus.text = "$dk 端口开放"
            } else {
                tv_portstatus.text = "$dk 端口未开放"
            }
        }
    }
}
